<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>最长公共前缀 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-dots {
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            line-height: 1.6;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .operator { color: #d4d4d4; }
        .number { color: #b5cea8; }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        .step-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            flex-shrink: 0;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">最长公共前缀</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">探索字符串数组中的共同起点</p>
            <div class="flex justify-center gap-4 text-sm">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-clock mr-2"></i>时间复杂度 O(S)
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-layer-group mr-2"></i>字符串算法
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.1s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <i class="fas fa-book-open text-3xl gradient-text mr-4"></i>
                    <h2 class="text-3xl font-bold">题目描述</h2>
                </div>
                <p class="text-lg text-gray-700 leading-relaxed">
                    编写一个函数来查找字符串数组中的最长公共前缀。这是一道经典的字符串处理题目，要求我们找出所有字符串共同的开头部分。
                </p>
            </div>
        </section>

        <!-- Visual Example -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.2s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h3 class="text-2xl font-bold mb-6 flex items-center">
                    <i class="fas fa-eye text-purple-600 mr-3"></i>
                    可视化示例
                </h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="font-semibold mb-4 text-gray-700">输入示例</h4>
                        <div class="space-y-2">
                            <div class="bg-gray-100 p-3 rounded-lg font-mono">["flower", "flow", "flight"]</div>
                            <div class="bg-gray-100 p-3 rounded-lg font-mono">["dog", "racecar", "car"]</div>
                        </div>
                    </div>
                    <div>
                        <h4 class="font-semibold mb-4 text-gray-700">输出结果</h4>
                        <div class="space-y-2">
                            <div class="bg-purple-100 p-3 rounded-lg font-mono text-purple-700">"fl"</div>
                            <div class="bg-purple-100 p-3 rounded-lg font-mono text-purple-700">""</div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Algorithm Flow -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.3s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h3 class="text-2xl font-bold mb-6 flex items-center">
                    <i class="fas fa-project-diagram text-purple-600 mr-3"></i>
                    算法流程图
                </h3>
                <div class="mermaid">
                    graph TD
                        A[开始] --> B[检查数组是否为空]
                        B -->|是| C[返回空字符串]
                        B -->|否| D[将第一个字符串作为初始前缀]
                        D --> E[遍历剩余字符串]
                        E --> F{当前字符串是否以前缀开头?}
                        F -->|是| G[继续下一个字符串]
                        F -->|否| H[缩短前缀]
                        H --> I{前缀是否为空?}
                        I -->|是| J[返回空字符串]
                        I -->|否| F
                        G --> K{是否遍历完所有字符串?}
                        K -->|否| E
                        K -->|是| L[返回最长公共前缀]
                        C --> M[结束]
                        J --> M
                        L --> M
                </div>
            </div>
        </section>

        <!-- Solution Steps -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.4s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h3 class="text-2xl font-bold mb-8 flex items-center">
                    <i class="fas fa-lightbulb text-purple-600 mr-3"></i>
                    解题思路
                </h3>
                <div class="space-y-6">
                    <div class="flex items-start gap-4">
                        <div class="step-number">1</div>
                        <div class="flex-1">
                            <h4 class="font-semibold text-lg mb-2">初始化前缀</h4>
                            <p class="text-gray-700">将第一个字符串作为初始的公共前缀，这是我们比较的基准。</p>
                        </div>
                    </div>
                    <div class="flex items-start gap-4">
                        <div class="step-number">2</div>
                        <div class="flex-1">
                            <h4 class="font-semibold text-lg mb-2">逐个比较</h4>
                            <p class="text-gray-700">遍历数组中的每个字符串，检查它是否以当前前缀开头。</p>
                        </div>
                    </div>
                    <div class="flex items-start gap-4">
                        <div class="step-number">3</div>
                        <div class="flex-1">
                            <h4 class="font-semibold text-lg mb-2">调整前缀</h4>
                            <p class="text-gray-700">如果某个字符串不以当前前缀开头，就逐步缩短前缀，直到匹配或前缀为空。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.5s;">
            <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8 pb-0">
                    <h3 class="text-2xl font-bold mb-6 flex items-center">
                        <i class="fas fa-code text-purple-600 mr-3"></i>
                        代码实现
                    </h3>
                </div>
                <div class="code-block mx-8 mb-8">
                    <div class="code-header">
                        <div class="code-dots">
                            <div class="dot dot-red"></div>
                            <div class="dot dot-yellow"></div>
                            <div class="dot dot-green"></div>
                        </div>
                        <span class="text-gray-400 text-sm">Python</span>
                    </div>
                    <pre><code><span class="keyword">def</span> <span class="function">longestCommonPrefix</span>(strs):
    <span class="keyword">if</span> <span class="keyword">not</span> strs:
        <span class="keyword">return</span> <span class="string">""</span>
    
    <span class="comment"># 将第一个字符串作为初始前缀</span>
    prefix = strs[<span class="number">0</span>]
    
    <span class="comment"># 遍历剩余的字符串</span>
    <span class="keyword">for</span> string <span class="keyword">in</span> strs[<span class="number">1</span>:]:
        <span class="comment"># 当前字符串不以prefix开头时，缩短prefix</span>
        <span class="keyword">while</span> string[:len(prefix)] != prefix:
            prefix = prefix[:-<span class="number">1</span>]
            <span class="keyword">if</span> <span class="keyword">not</span> prefix:
                <span class="keyword">return</span> <span class="string">""</span>
    
    <span class="keyword">return</span> prefix</code></pre>
                </div>
            </div>
        </section>

        <!-- Complexity Analysis -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.6s;">
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-clock text-4xl text-purple-600 mr-4"></i>
                        <div>
                            <h4 class="text-xl font-bold">时间复杂度</h4>
                            <p class="text-3xl font-bold gradient-text">O(S)</p>
                        </div>
                    </div>
                    <p class="text-gray-700 mt-4">其中 S 是所有字符串的总长度。最坏情况下，我们需要比较所有字符。</p>
                </div>
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-memory text-4xl text-purple-600 mr-4"></i>
                        <div>
                            <h4 class="text-xl font-bold">空间复杂度</h4>
                            <p class="text-3xl font-bold gradient-text">O(1)</p>
                        </div>
                    </div>
                    <p class="text-gray-700 mt-4">只使用了常数级别的额外空间来存储前缀变量。</p>
                </div>
            </div>
        </section>

        <!-- Key Points -->
        <section class="animate-fadeInUp" style="animation-delay: 0.7s;">
            <div class="